<template>
	<view class="layout">
		<!-- 对搜索框进行浮动 -->
		<view class="navbar">
			<view class="statusBar" :style="{height:getStatusBarHeight()+'px'}"></view>
			<view class="titleBar" :style="{height:getTitleBarHeight()+'px', marginLeft:getLeftIconLeft()+'px'}" >
				<uni-icons @click="goBack" type="left" size="25"></uni-icons>
				<view class="search" @click="goSearch" >
					<uni-icons class="icon" type="search" color="#888" size="18"></uni-icons>
					<text class="text">搜索</text>
				</view>
				<text @click="goSearch"  style="margin-left: 20rpx;">搜索</text>
			</view>
		</view>
		<!-- 填充区域->为了让固定的搜索框不遮盖内容 -->
		<view class="fill" :style="{height:getNavBarHeight() +'px'}">
					
		</view>
	</view>
	
	<!-- 左边 -->
	<view class="left">
		<view class="list">
			<view class="tab" @click="isShows(1)" >
				<text :class="index==1?'active':''">男生</text>
			</view>
			<view class="tab" @click="isShows(2)" >
				<text :class="index==2?'active':''">女生</text>
			</view>
			<view class="tab" @click="isShows(3)" >
				<text :class="index==3?'active':''">听书</text>
			</view>
		</view>
	</view>
	
	
	<!-- 右边 -->
	<view class="right">

		<view class="content" >	
			<view class="items" v-for="item in dataList" :key="item.id"   @click="goList(item.categoryName)">
				<!-- 左边 -->
				<view class="l">
					<text class="type">{{item.categoryName}}</text>
					<text class="nums">{{item.count}}本</text>
				</view>
				<!-- 右边 -->
				<view class="r">
					<image :src="item.image"></image>
				</view>
			</view>	
		</view>
	</view>
</template>

<script setup>
import { onMounted, ref, watch } from "vue"
import { getStatusBarHeight, getTitleBarHeight, getNavBarHeight, getLeftIconLeft } from "@/utils/system.js"
import {ApigetCategroys} from "../api/novel.js"
// 定义一个变量用来计算索引
const index = ref(1)

// 定义一个点击事件
const isShows = (value) => {
	index.value = value
	console.log(value)
	getAllCategroy(value)
}

// 模拟数据
const dataList = ref([])
//获取所有的分类
const getAllCategroy = async(type) => {
	let res = await ApigetCategroys(type)
	dataList.value = res
}

// 监听 index 的变化
watch(index, (newVal) => {
	getAllCategroy(newVal)
})

onMounted(() => {
	//获取所有的分类
	getAllCategroy(index.value)
})
// 跳转到列表页面
const goList = (type) => {
	uni.navigateTo({
		url: `/pages/novel/bookList?type=` + type
	})
}

// 跳转到搜索界面
const goSearch = () => {
	uni.navigateTo({
		url: "/pages/novel/search"
	})
}

// 返回
const goBack = () => {
	uni.navigateBack()
}
</script>

<style lang="scss" scoped>
	
	.layout {
		background: #fff;
		.navbar {
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			z-index: 10;
			background: #fff;
			border: 1px solid #eee;
			border-radius: 10rpx;
			box-shadow: 0 0 30rpx rgba(0, 0, 0, 0.05);
			.titleBar {
				display: flex;
				align-items: center;
				padding: 0 30rpx;
				.title {
					color: #000;
					font-size: 30rpx;
					font-weight: 700;
				}
				.search {
					width: auto;
					flex: 1;
					height: 50rpx;
					border-radius: 60rpx;
					background: rgba(255, 255, 255, 0.4);
					border: 1px solid #eee;
					margin-left: 30rpx;
					font-size: 28rpx;
					display: flex;
					align-items: center;
					.icon {
						margin-left: 5rpx;
					}
					.text {
						padding-left: 10rpx;
					}
				}
			}
			
		}
		
	}
	
	.left {
		position: fixed;
		min-height: 900px;
		width: 20%;
		background: #fff;
		border: 1px solid #eee;
		border-radius: 10rpx;
		box-shadow: 0 0 30rpx rgba(0, 0, 0, 0.05);
	}
	
	.right {
		height: 900px;
		width: 100%;
		background-color: #efefee;
	}
	
	.content {
		position: fixed;
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 5rpx;
		margin: 0 170rpx 0;
		.items {
			background: #fff;
			margin: 20rpx 0;
			display: flex;
			justify-content: space-between;
			width: 260rpx;
			border-radius: 5px;
			margin: 20rpx 10rpx 10rpx;
			.r {
				image {
					width: 75rpx;
					height: 100rpx;
					padding: 10rpx;
				}
			}
			.l {
				display: flex;
				flex-direction: column;
				.type {
					font-size: 14px;
					padding: 20rpx 10rpx 5rpx;
				}
				.nums {
					color: #999;
					font-size: 12px;
					padding: 0 10rpx 5rpx;
				}
			}
		}
		
	}
	
	.list {
		margin-top: 20rpx;
		display: flex;
		flex-direction: column;
		margin-left: 40rpx;
		.tab {
			padding: 10rpx 0;
			font-size: 15px;
			.active {
				font-size: 17px;
				font-weight: 600;
				color: #4cd964;
			}
		}
		
	}

</style>